# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import pickle
import re
from openpyxl import load_workbook

def createPickle(targetFunc,inputFile,outputFile):
    res=targetFunc(inputFile)
    output=open(outputFile, 'wb')
    pickle.dump(res, output)
    output.close()

def readPickle(inputFile):
    pkl_file = open(inputFile, 'rb')
    Info = pickle.load(pkl_file)
    pkl_file.close()
    return Info

def readDanYiFuInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'单义符')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    half=[]
    full=[]
    for i in xrange(1,nrowsP):
        oneCol=rowInfoP[i][1].value
        twoCol=rowInfoP[i][2].value
        if oneCol!=None:
            if oneCol==u"+":
                half.append(u"\\"+oneCol)
            elif oneCol==u".":
                half.append(u"\\"+oneCol)
            elif oneCol==u"?":
                half.append(u"\\"+oneCol)
            elif oneCol==u"\\":
                half.append(u"\\"+oneCol)
            elif oneCol==u"|":
                half.append(u"\\"+oneCol)
            else:
                half.append(oneCol)
        if twoCol !=None:
            full.append(twoCol)
    res={}
    res['half']=half
    res['full']=full
    return res

def readDuoYiFuInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'多义符')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    half=[]
    full=[]
    for i in xrange(1,nrowsP):
        oneCol=rowInfoP[i][1].value
        twoCol=rowInfoP[i][2].value
        if oneCol!=None:
            if oneCol==u"-":
                half.append(u"\\"+oneCol)
            else:
                half.append(oneCol)
        if twoCol !=None:
            full.append(twoCol)
    res={}
    res['half']=half
    res['full']=full
    return res

def readChengDuifuInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'成对符')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    half=[]
    full=[]
    for i in xrange(1,nrowsP):
        oneCol=rowInfoP[i][1].value
        twoCol=rowInfoP[i][2].value
        if oneCol!=None:
            half.append(oneCol)
        if twoCol !=None:
            # if twoCol == '[ ]':
            #     print twoCol
            #     print type(twoCol)
            full.append(twoCol)
    res={}
    res['half']=half
    res['full']=full
    return res

def readNumInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'数字')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)


    Num=[]
    for i in xrange(1,nrowsP):
        num=rowInfoP[i][4].value
        if num!=None:
            Num.append(str(num))
    res={}
    res['half']=Num
    return res

def readCharacterInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'英文字母')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)


    character=[]
    for i in xrange(1,nrowsP):
        char=rowInfoP[i][4].value
        if char!=None:
            character.append(char)
    res={}
    res['half']=character
    return res

def readAddressInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'地址')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    address=[]
    for i in xrange(1,nrowsP):
        adds=rowInfoP[i][1].value
        if adds!=None:
            address.append(adds)
    return address

def readEmailInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'邮箱')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    email=[]
    for i in xrange(1,nrowsP):
        em=rowInfoP[i][1].value
        if em!=None:
            email.append(em)
    return email

def readUrlInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'网址')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    url=[]
    for i in xrange(1,nrowsP):
        tmp=rowInfoP[i][1].value
        if tmp!=None:
            url.append(tmp)
    return url

def readQqInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'QQ')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    Qq=[]
    for i in xrange(1,nrowsP):
        qq=rowInfoP[i][1].value
        if qq!=None:
            Qq.append(qq)
    return Qq

def readPhoneInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'手机')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    phone=[]
    for i in xrange(1,nrowsP):
        p=rowInfoP[i][1].value
        if p!=None:
            phone.append(p)
    return phone

def readTelphoneInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'座机')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    telphone=[]
    for i in xrange(1,nrowsP):
        tp=rowInfoP[i][1].value
        if tp!=None:
            telphone.append(tp)
    return telphone

def readWeichatInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'微信')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    weichat=[]
    for i in xrange(1,nrowsP):
        wc=rowInfoP[i][1].value
        if wc!=None:
            weichat.append(wc)
    return weichat

def readDomainInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'域名')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    domainList=[]
    for i in xrange(1,nrowsP):
        domain=rowInfoP[i][1].value
        if domain!=None:
            domainList.append(domain)
    return domainList


def readSpecialNameInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'联系人')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    specialName=[]
    for i in xrange(1,nrowsP):
        sn=rowInfoP[i][1].value
        if sn!=None:
            specialName.append(sn)
    return specialName

def readCompanyNameInfo(file):
    workbook_R=load_workbook(file)

    worksheet_P=workbook_R.get_sheet_by_name(u'企业名')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    companyName=[]
    for i in xrange(1,nrowsP):
        cn=rowInfoP[i][1].value
        if cn!=None:
            companyName.append(cn)
    return companyName

def readProviceInfo(file):
    workbook_R=load_workbook(file)
    worksheet_P=workbook_R.get_sheet_by_name(u'1级-省及直辖市')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    proviceName=[]
    for i in xrange(2,nrowsP):
        provice=rowInfoP[i][2].value
        if provice!=None:
            proviceList=provice.split(';')
            for each in proviceList:
                each=each.strip()
                if len(each)!=0:
                    proviceName.append(each)
    return proviceName

def readCityInfo(file):
    workbook_R=load_workbook(file)
    worksheet_P=workbook_R.get_sheet_by_name(u'2级-地级市')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    cityListName=[]
    for i in xrange(1,nrowsP):
        city=rowInfoP[i][3].value
        if city!=None:
            cityList=city.split(';')
            for each in cityList:
                each=each.strip()
                if len(each)!=0:
                    cityListName.append(each)
    return cityListName

def readCountyInfo(file):
    workbook_R=load_workbook(file)
    worksheet_P=workbook_R.get_sheet_by_name(u'3级-区县和县级市')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    countyListName=[]
    for i in xrange(3,nrowsP):
        county=rowInfoP[i][3].value
        if county!=None:
            countyList=county.split(';')
            for each in countyList:
                each=each.strip()
                if len(each)!=0:
                    countyListName.append(each)
    return countyListName

def readCompanySuffixInfo(file):
    workbook_R=load_workbook(file)
    worksheet_P=workbook_R.get_sheet_by_name(u'总')

    rowInfoP=worksheet_P.rows
    nrowsP=len(rowInfoP)

    suffixListName=[]
    for i in xrange(1,nrowsP):
        suffix2=rowInfoP[i][0].value
        if suffix2!=None:
            suffix2=suffix2.strip()
            suffixListName.append(suffix2)

        suffix3=rowInfoP[i][2].value
        if suffix3!=None:
            suffix3=suffix3.strip()
            suffixListName.append(suffix3)

        suffix4=rowInfoP[i][4].value
        if suffix4!=None:
            suffix4=suffix4.strip()
            suffixListName.append(suffix4)

        suffix5=rowInfoP[i][6].value
        if suffix5!=None:
            suffix5=suffix5.strip()
            linshi1=re.sub('\(','\(',suffix5)
            linshi2=re.sub('\)','\)',linshi1)
            suffixListName.append(linshi2)

        # suffix6=rowInfoP[i][8].value
        # if suffix6!=None:
        #     suffix6=suffix6.strip()
        #     suffixListName.append(suffix6)

        # suffix7=rowInfoP[i][10].value
        # if suffix7!=None:
        #     suffix7=suffix7.strip()
        #     suffixListName.append(suffix7)

        # suffix8=rowInfoP[i][12].value
        # if suffix8!=None:
        #     suffix8=suffix8.strip()
        #     suffixListName.append(suffix8)
    return suffixListName

if __name__=='__main__':
    #生成单义符pickle文件
    # inputFile='F:\\target\\zifuji828.xlsx'
    # outputFile='F:\\target\\danyifu828.pkl'
    # createPickle(readDanYiFuInfo,inputFile,outputFile)
    #生成多义符pickle文件
    # inputFile=''
    # outputFile='F:\\target\\duoyifu828.pkl'
    # createPickle(readDuoYiFuInfo,inputFile,outputFile)

    #生成成对符pickle文件
    # inputFile='F:\\target\\zifuji828.xlsx'
    # outputFile='F:\\target\\chengduifu828.pkl'
    # createPickle(readChengDuifuInfo,inputFile,outputFile)

    # 生成数字pickle文件
    # inputFile='F:\\target\\zifuji828.xlsx'
    # outputFile='F:\\target\\number828.pkl'
    # createPickle(readNumInfo,inputFile,outputFile)

    # 生成字母pickle文件
    # inputFile='F:\\target\\zifuji828.xlsx'
    # outputFile='F:\\target\\character828.pkl'
    # createPickle(readCharacterInfo,inputFile,outputFile)

    # 生成地址pickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\address828.pkl'
    # createPickle(readAddressInfo,inputFile,outputFile)
    #
    # 生成邮箱pickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\email828.pkl'
    # createPickle(readEmailInfo,inputFile,outputFile)

    # 生成网址pickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\url828.pkl'
    # createPickle(readUrlInfo,inputFile,outputFile)

    # 生成QQpickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\qq828.pkl'
    # createPickle(readQqInfo,inputFile,outputFile)

    # 生成手机pickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\phone828.pkl'
    # createPickle(readPhoneInfo,inputFile,outputFile)

    # 生成座机pickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\telphone828.pkl'
    # createPickle(readTelphoneInfo,inputFile,outputFile)

    # 生成微信pickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\weichat828.pkl'
    # createPickle(readWeichatInfo,inputFile,outputFile)

    # 生成特例名pickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\specialName828.pkl'
    # createPickle(readSpecialNameInfo,inputFile,outputFile)

    # 生成企业名称pickle文件
    # inputFile='F:\\target\\changyongci828.xlsx'
    # outputFile='F:\\target\\companyName828.pkl'
    # createPickle(readCompanyNameInfo,inputFile,outputFile)

    # 生成省份名称pickle文件
    # inputFile='F:\\target\\xingzhengquhua828.xlsx'
    # outputFile='F:\\target\\provice828.pkl'
    # createPickle(readProviceInfo,inputFile,outputFile)

    # 生成地市份名称pickle文件
    # inputFile='F:\\target\\xingzhengquhua828.xlsx'
    # outputFile='F:\\target\\city828.pkl'
    # createPickle(readCityInfo,inputFile,outputFile)

    # 生成县区名称pickle文件
    # inputFile='F:\\target\\xingzhengquhua828.xlsx'
    # outputFile='F:\\target\\county828.pkl'
    # createPickle(readCountyInfo,inputFile,outputFile)

    # 生成公司后缀pickle文件
    # inputFile='F:\\target\\companysuffix828.xlsx'
    # outputFile='F:\\target\\companysuffix828.pkl'
    # createPickle(readCompanySuffixInfo,inputFile,outputFile)

    # 生成域名pickle文件
    inputFile='F:\\target\\changyongci828.xlsx'
    outputFile='F:\\target\\domain828.pkl'
    createPickle(readDomainInfo,inputFile,outputFile)


    #测试pickle文件
    pkl_file = open('F:\\target\\domain828.pkl', 'rb')
    Info = pickle.load(pkl_file)
    pkl_file.close()

    # for item in Info:
    #     print item
    print Info